package net.hzlj.platform.enterprise.task;

import android.util.Log;

import com.orhanobut.logger.Logger;

import net.hzlj.platform.enterprise.BootApp;
import net.hzlj.platform.enterprise.beans.resp.RespBaseBean;
import net.hzlj.platform.enterprise.common.Constant;
import net.hzlj.platform.enterprise.config.HttpConfig;
import net.hzlj.platform.enterprise.utils.GsonUtils;
import net.hzlj.platform.enterprise.utils.NetworkUtils;

import java.util.Date;

import cn.finalteam.okhttpfinal.BaseHttpRequestCallback;
import cn.finalteam.okhttpfinal.HttpCycleContext;
import cn.finalteam.okhttpfinal.HttpRequest;
import okhttp3.Headers;
import okhttp3.OkHttpClient;

/**
 * @ClassName: HttpRequestTask
 * @Description: 网络请求
 * @Author: Jason.Zhang zhangyujn1989ok@gmail.com
 * @Date: 2015-7-28 14:30:55
 */
public class HttpRequestTask<T> extends Task {

    private static final String TAG = HttpRequestTask.class.getSimpleName();

    public HttpRequestTask(TaskListener listener,Class<T> tClass){
        super(listener,tClass);
    }

    public HttpRequestTask(TaskListener listener,HttpCycleContext httpCycleContext){
        super(listener,httpCycleContext);
    }

    public HttpRequestTask(TaskListener listener,HttpCycleContext httpCycleContext,Class<T> tClass){
        super(listener,httpCycleContext,tClass);
    }


    @Override
    public void execute() {
        final int actionType = type;
        HttpRequest.post(url,params,new BaseHttpRequestCallback<String>(){

            @Override
            protected void onSuccess(String o) {
                super.onSuccess(o);
                RespBaseBean<T> bean = GsonUtils.fromJson(o,clas);
                resData = bean;
                isSuccess = true;
                rspDesc = bean.getRespMsg();
                listener.onTaskResult(HttpRequestTask.this);
                BootApp.getInstance().updateFaceNative(actionType);
            }

            @Override
            public void onResponse(String response, Headers headers) {
            }

            @Override
            public void onFailure(int errorCode, String msg) {
                super.onFailure(errorCode, msg);
                isSuccess = false;
                if (!BootApp.getInstance().isConnect()){
                    rspDesc = "网络不给力，请检查后重试";
                }else {
                    rspDesc = "服务器异常";
                }
                listener.onTaskResult(HttpRequestTask.this);
            }
        });

    }

}
